#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ssm.h"


void printCar(Car source)
{
	printf("A      		= %f\n", source.A);
	printf("C_d    		= %f\n", source.C_d);
	printf("C_rr   		= %f\n", source.C_rr);
	printf("W      		= %f\n", source.W);
	printf("M		= %f\n", source.M);
	printf("B_C		= %f\n", source.B_C);
	printf("B_CRate		= %f\n", source.B_CRate);
	printf("B_CPeukert	= %f\n", source.B_CPeukert);
	printf("B_n		= %f\n", source.B_n);
	printf("P_arrayMax	= %f\n", source.P_arrayMax);
	printf("M_d		= %f\n", source.M_d);
	printf("M_r		= %f\n", source.M_r);
	printf("P_p		= %f\n", source.P_p);	
}

int writeCar(char* filename, Car *source)
{
	FILE *fp;
	if((fp=fopen(filename, "wb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for output\n", filename);
  		exit(1);
	}
	if((fwrite(source, sizeof(Car), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}

int readCar(char* filename, Car *dest)
{
	FILE *fp;
	if((fp=fopen(filename,"rb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for input\n", filename);
  		exit(1);
	}
	if((fread(dest, sizeof(Car), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}
